package com.zhiyin.seal.service.impl;

import com.sun.management.OperatingSystemMXBean;
import com.zhiyin.seal.dao.AdminWebDao;
import com.zhiyin.seal.entity.SystemLogEntity;
import com.zhiyin.seal.entity.SystemUserEntity;
import com.zhiyin.seal.entity.bo.ServerStatsBo;
import com.zhiyin.seal.entity.bo.SystemLogBo;
import com.zhiyin.seal.entity.bo.SystemRoleBo;
import com.zhiyin.seal.entity.bo.SystemUserBo;
import com.zhiyin.seal.service.AdminWebService;
import com.zhiyin.seal.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.management.ManagementFactory;
import java.text.DecimalFormat;
import java.util.List;

@Service
public class AdminWebServiceImpl implements AdminWebService {

    @Autowired
    AdminWebDao adminWebDao;

    @Override
    public SystemUserBo login(String userName, String password) {
        SystemUserEntity userEntity = adminWebDao.selectSystemUserByNameAndPwd(userName,password);
        return BeanUtil.copy(userEntity,SystemUserBo.class);
    }

    @Override
    public List<SystemRoleBo> getRoleList() {
        return BeanUtil.copyList(adminWebDao.selectRoleList(),SystemRoleBo.class);
    }

    @Override
    public Boolean addRole(SystemRoleBo roleBo) {
        return adminWebDao.insertRole(roleBo.getName()) > 0;
    }

    @Override
    public List<Long> getRoleRightIdList(Long roleId) {
        return adminWebDao.selectRoleRightIdList(roleId);
    }

    @Override
    public Boolean updateRoleRight(Long roleId, List<Long> menuIds) {
        return adminWebDao.insertOrUpdateRoleRight(roleId,menuIds) > 0;
    }

    @Override
    public Boolean deleteRole(Long roleId) {
        return adminWebDao.deleteRole(roleId) > 0;
    }

    @Override
    public List<SystemUserBo> getSystemUserList() {
        return BeanUtil.copyList(adminWebDao.selectUserList(),SystemUserBo.class);
    }

    @Override
    public SystemUserBo getSystemUserByName(String userName) {
        return BeanUtil.copy(adminWebDao.selectSystemUserByName(userName),SystemUserBo.class);
    }

    @Override
    public Boolean addSystemUser(SystemUserBo systemUserBo) {
        return adminWebDao.insertSystemUser(BeanUtil.copy(systemUserBo,SystemUserEntity.class)) > 0;
    }

    @Override
    public Boolean modifyUserPassword(Long userId, String pwd) {
        return adminWebDao.updateSystemUserPassword(userId,pwd) > 0;
    }

    @Override
    public Boolean changeUserRole(Long userId, Long roleId) {
        return adminWebDao.updateUserRole(userId,roleId) > 0;
    }

    @Override
    public Boolean deleteUser(Long userId) {
        return adminWebDao.deleteSystemUser(userId) > 0;
    }

    @Override
    public ServerStatsBo getServerStatus() {
        OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 获取内存总容量
        long totalMemorySize = mem.getTotalPhysicalMemorySize();
        // 获取可用内存容量(剩余物理内存）
        long freeMemorySize = mem.getFreePhysicalMemorySize();
        // 计算内存使用率
        String usedRAM = new DecimalFormat("#.##%").format(((totalMemorySize - freeMemorySize) * 1.0 / totalMemorySize));
        // 获取进程CPU使用率
        String cpuRate= new DecimalFormat("#.##%").format(mem.getProcessCpuLoad());

        ServerStatsBo serverStatsBo = new ServerStatsBo();
        serverStatsBo.setCupUtilization(cpuRate);
        serverStatsBo.setMemorySize(new DecimalFormat("#.##").format(totalMemorySize / 1024.0 / 1024 / 1024) + "GB");
        serverStatsBo.setMemoryUtilization(usedRAM);

        return serverStatsBo;
    }

    @Override
    public Boolean addSystemLog(SystemLogBo systemLogBo) {
        return adminWebDao.insertSystemLog(BeanUtil.copy(systemLogBo, SystemLogEntity.class)) > 0;
    }

    @Override
    public List<SystemLogBo> getSystemLogList() {
        return BeanUtil.copyList(adminWebDao.selectSystemLog(),SystemLogBo.class);
    }


}
